﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using fingerWinForm.Excel;
using System.IO;
using fingerWinForm.Algorithms.SVM;

using fingerWinForm.Utilities;


namespace fingerWinForm.GUI
{
    public partial class CreateFilesWindow : Form
    {
        FileMgr fileManager;
        objForgenerateRandomVectorFile obj1;
 
        

        public CreateFilesWindow(FileMgr fileManagerPassed)
        {
            InitializeComponent();
            fileManager = fileManagerPassed;
            obj1 = new objForgenerateRandomVectorFile();
            VectorOperations.scaleAraysInit(ref obj1);
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            Close();
        }

        public static void Empty(System.IO.DirectoryInfo directory)
        {
            foreach (System.IO.FileInfo file in directory.GetFiles()) 
                file.Delete();
            foreach (System.IO.DirectoryInfo subDirectory in directory.GetDirectories()) 
                subDirectory.Delete(true);
        }
        private void createButton_Click(object sender, EventArgs e)
        {
            if (threeClassesCheckBox.Checked)
            {
                //we are using three classes and not 5
                VectorOperations.numOfClasses = 3;
            }
            else
            {
                VectorOperations.numOfClasses = 5;
            }


            int[] classcounterArray = new int[VectorOperations.numOfClasses];
            string[] vectorsfordup = new string[VectorOperations.numOfClasses];

            VectorOperations.intArrayInit(ref classcounterArray);
            VectorOperations.dupArrayInit(ref vectorsfordup);
           
            if (CheckInput.isEmpty(comboBox.Text))
            {MessageBox.Show("You must choose type of creating files", "Error", MessageBoxButtons.OK,MessageBoxIcon.Error);}
            else
            {
                string pathForCheckAndClean = @"C:\parkinsonTool\svm\finger Taps";
                System.IO.DirectoryInfo directory = new System.IO.DirectoryInfo(pathForCheckAndClean);
                Empty(directory); //clean this directory.

                pathForCheckAndClean = @"C:\parkinsonTool\svm\hand Movements";
                directory = new System.IO.DirectoryInfo(pathForCheckAndClean);
                Empty(directory); //clean this directory.

                fileManager.createTrainfile("finger Taps", obj1);    //build train file with all the vectors
                fileManager.createTrainfile("hand Movements", obj1); //build train file with all the vectors

                if (scalingCheckBox.Checked)
                {
                    VectorOperations.helpscalingCheckBoxChecked(ref obj1);
                    fileManager.createTrainfile("finger Taps", obj1);    //rebuild train file with all the vectors
                    fileManager.createTrainfile("hand Movements", obj1); //rebuild train file with all the vectors
                }

                if (comboBox.Text == "30% Predict / 70% Train")
                {
                    for (int i = 0; i < 20; i++)
                    {
                        obj1.index = i;
                        System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(VectorOperations.generateRandomVectorFilesWrapper), obj1);
                    }
                    MessageBox.Show("finished creating the train and predict files", "My Application", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);

                }
                else if (comboBox.Text == "Leave One Out")
                {
                     obj1.index = -1;
                     System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(VectorOperations.generateRandomVectorFilesWrapper), obj1);
                }

                if (overSamplingCheckBox.Checked)
                {
                    System.Threading.Thread.Sleep(4000);
                    VectorOperations.helpOverSampllingkBoxChecked(ref classcounterArray, ref vectorsfordup, "finger Taps_trainfile.train", "finger Taps");
                    VectorOperations.OverSampligAddVectorsToPredictAndTrainFiles(ref classcounterArray, ref vectorsfordup, "finger Taps_trainfile.train", "finger Taps");

                    VectorOperations.intArrayInit(ref classcounterArray);
                    VectorOperations.dupArrayInit(ref vectorsfordup);
                  
                    VectorOperations.helpOverSampllingkBoxChecked(ref classcounterArray, ref vectorsfordup, "hand Movements_trainfile.train", "hand Movements");
                    VectorOperations.OverSampligAddVectorsToPredictAndTrainFiles(ref classcounterArray, ref vectorsfordup, "hand Movements_trainfile.train", "hand Movements");
                }

              }//else

                Close();
            }
            
            
        }
    
}
